#include "HMM_Helper.h"
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include <algorithm>
#include <iterator>

using namespace std;


HMM_Helper::HMM_Helper(void)
{
}


HMM_Helper::~HMM_Helper(void)
{
}

vector<double> HMM_Helper::ConvertToDoubleVectorLog(std::string s)
{
	vector<double> v;
	vector<string> tokens = Split(s);

	for (unsigned int i=0; i<tokens.size(); i++)
		v.push_back(log(atof(tokens[i].c_str())));

	return v;
}

vector<double> HMM_Helper::ConvertToDoubleVector(std::string s)
{
	vector<double> v;
	vector<string> tokens = Split(s);

	for (unsigned int i=0; i<tokens.size(); i++)
		v.push_back(atof(tokens[i].c_str()));

	return v;
}

double* HMM_Helper::ConvertToDoubleArray(std::string s)
{
	return ConvertToDoubleArray(Split(s));
}

double* HMM_Helper::ConvertToDoubleArray(vector<std::string> tokens)
{
	double* res = new double[tokens.size()];

	for (unsigned int i=0; i<tokens.size(); i++)
		res[i] = (atof(tokens[i].c_str()));

	return res;
}

double** HMM_Helper::ReadMatrix(ifstream& is, int rows, int columns)
{
	string line;
	vector<string> tokens;
	double** m = new double*[rows];

	for (int i=0; i <rows; i++)
	{
			getline (is,line);

			// split string..
			istringstream iss(line);
			copy(istream_iterator<string>(iss),
				istream_iterator<string>(),
				back_inserter<vector<string> >(tokens));

			m[i] = ConvertToDoubleArray(tokens);
	}

	return m;
}

std::vector<std::string> HMM_Helper::Split(std::string s) 
{	
	vector<string> tokens;

	// split string..
	istringstream iss(s);
	copy(istream_iterator<string>(iss),
		istream_iterator<string>(),
		back_inserter<vector<string> >(tokens));

	return tokens;
}
